# -*- encoding: utf-8 -*-
import mediapipe as mp
import cv2, math
from rect_img import RectImg
from button import ImgButton
from ocr import ImgOCR
import threading

hands = mp.solutions.hands
draw = mp.solutions.drawing_utils
styleDot = draw.DrawingSpec(color=(252, 191, 0), thickness=3)  # 设置样式
styleLine = draw.DrawingSpec(color=(255, 255, 255), thickness=2)  # 线的样式
# 设置hands参数
mp_hand = hands.Hands(min_tracking_confidence=0.5, min_detection_confidence=0.7, max_num_hands=2)
p_ocr = ImgOCR()

width = 1280
height = 720
cap = cv2.VideoCapture(0)

# set 功  能:设置视频参数,设置视频的宽高值和摄像头有关使用笔记本内置摄像头时只能设置为 1280*720 以及 640*480，
# 哪怕设置值不同时都会自动校正过来，并且仍然返回 True
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

rect_img = RectImg(width, height)
# btn_img = ImgButton(width, height)
is_status = 0

thread_num = 0

while cap.isOpened():
    ret, img = cap.read()
    if not ret:
        continue

    img.flags.writeable = False
    # img = cv2.flip(img, 1)
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    result = mp_hand.process(imgRGB)
    if result.multi_hand_landmarks:
        is_status = 1
        for hs in result.multi_hand_landmarks:
            draw.draw_landmarks(img, hs, hands.HAND_CONNECTIONS, styleDot, styleLine)
            #
            # for i, lm in enumerate(hs.landmark):
            #     # print(i, lm.x * width, lm.y * height)  # 点坐标比例
            #     xpos = int(lm.x * width)
            #     ypos = int(lm.y * height)
            #     # 显示第几个点
            #     cv2.putText(img, str(i), (xpos - 25, ypos + 5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)

            y7 = int(hs.landmark[7].y * height)
            y12 = int(hs.landmark[12].y * height)
            y16 = int(hs.landmark[16].y * height)

            # print(y7, y12, y16, int(hs.landmark[17].x * width), int(hs.landmark[5].x * width))
            # 右手 画线
            if int(hs.landmark[17].x * width) > int(hs.landmark[5].x * width):
                x4 = int(hs.landmark[4].x * width)
                x2 = int(hs.landmark[2].x * width)
                if x2 - x4 > 50:
                    is_status = 0
                    rect_img.clean()
                    p_ocr.clean()
                    thread_num = 0
                elif y7 < y12 and y7 < y16 and is_status == 1:
                    y8 = int(hs.landmark[8].y * height)
                    x8 = int(hs.landmark[8].x * width)
                    img = rect_img.draw(img, x8, y8 - 10)
    else:
        is_status = 0
        rect_img.clean()
        p_ocr.clean()
        thread_num = 0

    # print("is_status:", is_status)
    # img = btn_img.draw(img)
    if is_status == 1:
        img, roi, img_reco_crop = rect_img.show_img(img)
        if thread_num < 3 and roi is not None:  # 念三遍
            threading.Thread(target=p_ocr.process_img, args=(roi,)).start()
            thread_num = thread_num + 1
        p_ocr.show_text()
        img = rect_img.draw_rect(img)

    cv2.imshow('img', img)
    if cv2.waitKey(1) == ord('q'):
        break

mp_hand.close()
cap.release()
cv2.destroyAllWindows()
